const http = require('http');
const url = require('url');
const querystring = require('querystring');
const db = require('./db');

const server = http.createServer((req, res) => {
    const parsedUrl = url.parse(req.url);
    const path = parsedUrl.pathname;
    const method = req.method;
    const query = querystring.parse(parsedUrl.query);
    let body = '';

    req.on('data', chunk => {
        body += chunk.toString();
    });

    req.on('end', () => {
        handleRequest(path, method, query, body, db, res);
    });
});

function handleRequest(path, method, query, body, db, res) {
    switch (path) {
        // 登录和注册的实现
        // 待办事项的增删改查的实现
        // ...
        default:
            res.writeHead(404, { 'Content-Type': 'text/plain' });
            res.end('Not Found');
            break;
    }
}

server.listen(3000, () => {
    console.log('Server running on http://localhost:3000/');
});